
const app = getApp()
Page({
  data: {
    pageNum:1,
    showRules:false,
    showNotice:false,
    mask_list:[],
    task_list: [],
    signCount:0, // 本月签到天数
    CurrentIndex:0,
    currentCoupon:{},
    date_log: [], // 已签到过的日期,
    signDay: '',
    calendarShow: false
  },
  onLoad: function (options) {
    let _this =this
    _this.getAccountSign()
    _this.getMaskList();
    _this.getTaskList();
    _this.getDateLogs()
    _this.setData({
      signDay: _this.getCurrentDay()
    })
  },
  // 获取当前日期
  getCurrentDay() {
    let date = new Date();
    let day = date.getFullYear() + '-' + this.checkTime(date.getMonth() + 1) + '-' + this.checkTime(date.getDate());
    return day;
  },
  checkTime(time) {
    let newTime = time;
    if (time < 10) {
      newTime = '0' + time;
    }
    return newTime;
  },
  // 获取选中的日期
  getDateLogs() {
    let _this = this;
    app.util.request({
      url: "api/getHaveSign",
      method: "post",
      success(response) {
        if (response.data.code == 0) {
          let arr = response.data.data;
          let newArr = [];
          arr.forEach(element => {
            newArr.push(element.createTime)
          })
          let tempArr = [...new Set(newArr)]
          _this.setData({
            // signCount: tempArr.length,
            date_log: tempArr,
            calendarShow: true
          })
        } else {
          _this.setData({
            // signCount: 0,
            date_log: [],
            calendarShow: true
          })
        }
      }
    })
  },
  // 日历选中组件
  onSelectDate(e) {
    this.setData({
      signDay: e.detail.currDayDetail
    })
  },
  closeNotice() {
    let that = this
    that.getCoupon()
    that.setData({
      showNotice: false
    })
  },
  closeRules(){
    let that = this;
    that.setData({
      showRules:false
    })
  },
  openRules(){
    let that = this;
    app.util.request({
      url: "api/notice/find",
      method: "post",
      success(response) {
        that.setData({
          ruleDetail: response.data.data.details,
          showRules:true
        })
      }
    })
  },
  toSign(){
    let _this = this;
    console.log()
    if (_this.data.signDay == _this.getCurrentDay()) {
      // 当天签到
      _this.currentSign()
    } else {
      // 补签
      app.util.request({
        url: "api/judgeSign",
        method: "post",
        data: {
          createTime: _this.data.signDay
        },
        success(response) {
          if (!response.data.data){
            wx.showModal({
              title: '提示',
              content: '当天还未签到，需要补签吗？',
              confirmText: '补签',
              success(res) {
                if (res.confirm) {
                  _this.repireSign()
                }
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '当天您已签到过，无需重复签到！',
              showCancel: false,
            })
          }
        }
      })
    }
  },
  // 当天签到
  currentSign() {
    let _this = this;
    app.util.request({
      url: "api/account/updatesignin",
      method: "post",
      success(response) {
        if (response.data.code==0){
          wx.showModal({
            title: '提示',
            content: '签到成功',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                _this.setData({
                  calendarShow: false
                })
                wx.showToast({
                  title: '加载中...',
                  icon: 'loading'
                })
                setTimeout(() => {
                  _this.getDateLogs();
                  _this.getAccountSign();
                  _this.getTaskList();
                  wx.hideLoading()
                }, 500)
              }
            }
          })
        } else{
          wx.showToast({
            icon:'none',
            title: response.data.msg,
          })
        }
      }
  })
  },
  // 补签
  repireSign() {
    let _this = this;
    app.util.request({
      url: "api/repairSign",
      method: "post",
      data: {
        createTime: _this.data.signDay
      },
      success(response) {
        if (response.data.code == 0) {
          wx.showModal({
            title: '提示',
            content: '签到成功',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                _this.setData({
                  calendarShow: false
                })
                wx.showToast({
                  title: '加载中...',
                  icon: 'loading'
                })
                setTimeout(() => {
                  _this.getDateLogs();
                  _this.getAccountSign();
                  _this.getTaskList();
                  wx.hideLoading()
                }, 500)
              }
            }
          })
        } else {
          wx.showToast({
            title: response.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  showCoupon(e){
    let _this = this;
    console.log(e)
    console.log(_this.data.task_list[e.currentTarget.dataset.index])
    // return
    _this.setData({
      taskId: e.currentTarget.dataset.taskid,
      discountsId:e.currentTarget.dataset.discountsid,
      CurrentIndex:e.currentTarget.dataset.index,
      currentCoupon:_this.data.task_list[e.currentTarget.dataset.index],
      showNotice:true,
    })
  },
  getCoupon(){
    let _this = this;
    app.util.request({
        url: "api/acquisition/add",
        method: "post",
        data: {
          taskId: _this.data.taskId,
          discountsId: _this.data.discountsId,
      },
        success(response) {
            if (response.data.code == 0) {
              wx.showToast({
                title: '领取成功',
              })
              _this.setData({
                mask_list:[],
                // showNotice: true
              })
              _this.getTaskList()
            }
        }
    })
  },
getMaskList(){
  let _this = this;
  wx.showLoading({
    title: '加载中',
  })
  app.util.request({
      url: "api/task/findAll",
      method: "post",
      data: {
        page: _this.data.pageNum,
        pageSize:10,
    },
      success(response) {
          if (response.data.code == 0) {
              _this.setData({
                 mask_list: [..._this.data.mask_list,...response.data.data.list]
              })
              if(_this.data.pageNum<response.data.data.pages){
                _this.data.pageNum++
                _this.getTaskList()
              }else{
                wx.hideLoading()
              }
          }
      }
  })
},
getTaskList() {
  let _this = this;
  wx.showLoading({
    title: '加载中',
  })
  app.util.request({
      url: "api/task/findUserTask",
      method: "post",
      success(response) {
        wx.hideLoading()
          if (response.data.code == 0) {
              _this.setData({
                 task_list: response.data.data
              })
          }
      }
  })
},
getAccountSign(){
  let _this = this;
  app.util.request({
      url: "api/account/findDay",
      method: "post",
      success(response) {
          if (response.data.code == 0) {
              _this.setData({
                signCount: response.data.data
              })
          } else {
            _this.setData({
              signCount: 0
            })
            wx.showToast({
              title: response.data.msg,
              icon: 'none'
            })
          }
      }
  })
}
})
